package ict.kth.se.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ict.kth.se.manager.CookieManager;
import ict.kth.se.manager.PersonManager;
import ict.kth.se.manager.SeekerManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 * Handles requests for the application's pages about person.
 */
@Controller
public class PersonController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	private SeekerManager seekerManager;
	private PersonManager personManager;
	
	@Autowired
	public PersonController(PersonManager personManager, SeekerManager seekerManager) {
		this.personManager = personManager;
		this.seekerManager = seekerManager;
	}

	
	/**
	 * This method gets an person by id
	 * @param personId persons id
	 * @param language chosen language
	 * @param request HTTPServletRequest
	 * @param response HTTPServletResponse
	 * @return modelandview
	 */
	@RequestMapping(value = "personDetails")
	public ModelAndView getPersonById(
			@RequestParam("id") Long personId,
			@RequestParam(value="language", required=false) String language,
			HttpServletRequest request, HttpServletResponse response) {
		try{
			//Create modelAndView to be sent to view
			ModelAndView modelAndView = new ModelAndView("seekerDetails");
			//Language
			modelAndView = CookieManager.handleLanguageCookie(modelAndView, request, response, language);
			
			modelAndView.addObject("person", personManager.getPersonById(personId));
			
			return modelAndView;
		}
		catch(Exception e){
			e.printStackTrace();
			//Create modelAndView to be sent to view for errors
			ModelAndView modelAndView = new ModelAndView("error");
			modelAndView.addObject("error", e.getMessage());
			return modelAndView;
		}
	}

	/**
	 * This method gets an person by id
	 * @param personId persons id
	 * @param language chosen language
	 * @param request HTTPServletRequest
	 * @param response HTTPServletResponse
	 * @return modelandview
	 */
	@RequestMapping(value = "deletePerson")
	public ModelAndView removePersonById(
			@RequestParam("id") Long personId,
			@RequestParam(value="language", required=false) String language,
			HttpServletRequest request, HttpServletResponse response) {
		try{
			//Create modelAndView to be sent to view
			ModelAndView modelAndView = new ModelAndView("adminPage");
			//Language
			modelAndView = CookieManager.handleLanguageCookie(modelAndView, request, response, language);
			
			personManager.removePersonById(personId);
			
			modelAndView.addObject("allSeekers", seekerManager.getAllSeekers());
			modelAndView.addObject("allRecruiters", personManager.getAllRecruiters());
			
			return modelAndView;
		}
		catch(Exception e){
			e.printStackTrace();
			//Create modelAndView to be sent to view for errors
			ModelAndView modelAndView = new ModelAndView("error");
			modelAndView.addObject("error", e.getMessage());
			return modelAndView;
		}
	}
	
}
